/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.paypaymentdetails.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.thinkgem.jeesite.modules.employee.service.EmployeeService;
import com.thinkgem.jeesite.modules.employee.service.OutsourceCostDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.CalendarUtils;
import com.thinkgem.jeesite.common.utils.Constons;
import com.thinkgem.jeesite.modules.employee.entity.Employee;
import com.thinkgem.jeesite.modules.employee.entity.SummaryAttendance;
import com.thinkgem.jeesite.modules.paypaymentdetails.dao.PaydayPaymentDetailsDao;
import com.thinkgem.jeesite.modules.paypaymentdetails.entity.PaydayPaymentDetails;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 薪资实付明细Service
 * @author zxl
 * @version 2017-08-12
 */
@Service
@Transactional(readOnly = true)
public class PaydayPaymentDetailsService extends CrudService<PaydayPaymentDetailsDao, PaydayPaymentDetails> {
	@Autowired
	private OutsourceCostDetailService outsourceCostDetailService;
	@Autowired
	private EmployeeService employeeService;

	public PaydayPaymentDetails get(String id) {
		return super.get(id);
	}
	
	public List<PaydayPaymentDetails> findList(PaydayPaymentDetails paydayPaymentDetails) {
		return super.findList(paydayPaymentDetails);
	}
	
	public Page<PaydayPaymentDetails> findPage(Page<PaydayPaymentDetails> page, PaydayPaymentDetails paydayPaymentDetails) {
		return super.findPage(page, paydayPaymentDetails);
	}
	
	@Transactional(readOnly = false)
	public void save(PaydayPaymentDetails paydayPaymentDetails) {
		super.save(paydayPaymentDetails);
	}
	
	@Transactional(readOnly = false)
	public void delete(PaydayPaymentDetails paydayPaymentDetails) {
		super.delete(paydayPaymentDetails);
	}
    @Transactional(readOnly = false)
	public void genToPaydayPaymentDetailsService(Constons.WarehouseType warehouse, Date startday, Date endDay) {
		//先去查询考勤汇总记录

		List<SummaryAttendance>list =outsourceCostDetailService.querySummaryAttendanceByList(warehouse.name(),startday,endDay);
		for(SummaryAttendance o:list){
			PaydayPaymentDetails details = new PaydayPaymentDetails();
			details.setCompany(o.getCompany());
			details.setOffice(o.getOffice());
			details.setGenDate(new Date());
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			details.setGenDateStr(CalendarUtils.formatDateByFormat(startday,format)+"_"+CalendarUtils.formatDateByFormat(endDay,format));
			Employee employee = employeeService.findOneByEmployeeNumer(o.getEmployeeNumber());
			details.setEmploee(employee);
			details.setHourlyStandard(Double.valueOf(employee.getHourlyStandard()));
			details.setVatRate(Double.valueOf(employee.getVatRate()));
			details.setQtTotal(o.getOvertimeHour());
			details.setQtHalfWages(o.getOvertimeHour()*details.getHourlyStandard()*1.5);
			details.setQtWages(details.getQtHalfWages());
			details.setHolidaysHours(o.getAnnualLeave()+o.getAntenatalLeave()+o.getCompassionateLeave()+o.getPaternityLeave()+o.getStickLeave()
					+o.getMarriageHoliday());
			details.setLegalHolidaysHours(0.0); //缺个法定家假日
			details.setLegalHolidaysWages(0.0); //缺个法定家假日
			details.setHolidaysHours(details.getHolidaysHours()*details.getHourlyStandard());
			details.setStandardHours(o.getNormalHour());
			details.setStandardWages(o.getNormalHour()*details.getHourlyStandard());
			details.setPaidSickLeaveHours(o.getStickLeave());
			details.setPaidSickLeaveWages(o.getStickLeave()*details.getHourlyStandard());
			details.setPaidAnnualLeaveHours(o.getAnnualLeave());
			details.setPaidAnnualLeaveWages(o.getAnnualLeave()*details.getHourlyStandard());
			details.setTuneOffHours(o.getPaidLeaveDay());//调休假
			details.setTuneOffWages(details.getTuneOffHours()*details.getHourlyStandard());
			details.setFuneralLeaveHours(o.getFuneralLeaveDay());//丧假
			details.setFuneralLeaveWages(details.getFuneralLeaveHours()*details.getHourlyStandard());
			details.setMaternityLeaveHours(o.getAntenatalLeave());
			details.setMaternityLeaveWages(o.getAntenatalLeave()*details.getHourlyStandard());
			details.setOtherHolidaysHours(o.getOtherLeaveDay());//其它假期
			details.setOtherHolidaysWages(details.getOtherHolidaysHours()*details.getHourlyStandard());
			details.setQtQuarterHours(0.0);
			details.setQtQuarterWages(details.getQtQuarterHours()*details.getHourlyStandard()*1.25);
			details.setQtHalfHours(0.0);
			details.setQtHalfWages(details.getQtHalfHours()*details.getHourlyStandard()*1.5);
			details.setQtTwoHours(0.0);
			details.setQtTwoWages(details.getQtTwoHours()*details.getHourlyStandard()*2);
			details.setQtTwoPointTwoFiveHours(0.0);
			details.setQtTwoPointTwoFiveWages(details.getQtTwoPointTwoFiveHours()*details.getHourlyStandard()*2.5);
			details.setQtThreeHours(0.0);
			details.setQtThreeWages(details.getQtThreeHours()*details.getHourlyStandard()*3);

			//判断是否已经生成过
			String id = dao.getIdByGenDateStrAndEmpleeNumer(details.getGenDateStr(),details.getEmploee().getNumber());
			if(id!=null){
				details.setId(id);
				details.preUpdate();
			}
			User user = new User();
			user.setId("1");

			details.setCreateBy(user);
			details.setUpdateBy(user);
			details.setCreateDate(new Date());
			details.setUpdateDate(new Date());

			super.save(details);
		};
	}
}